<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

//发布者
class Ordersell extends Controller
{
    //发布者全部订单
    public function getbuylist()
    {
        $uid = Session::get('uid');
        $page = input('page', 1);
        $pagesax = input('pagesax', 4);
        $status = input('status');
        $where['uid'] = $uid;
        if ($status != null) {
            $where['status'] = $status;
        }
        $where['valid'] = 1;
        $res = Db::name('bill_info')->where($where)->order('created_time desc')->select();
        $view = page_array($pagesax, $page, $res, 1);

        if ($view['data'] == null) {
            return msg(1, '暂无数据', $view);
            exit();
        } else {
            return msg(1, '发布者全部订单', $view);
            exit();
        }
    }


    //支付服务费
    public function addservice_charge()
    {
        //$uid = Session::get('uid');
        $uid = 1;
        $id = input('id');
        $bill_no = input('bill_no');
        if ($bill_no == null) {
            return msg(2, '缺少主要参数', $id);
            exit();
        }
        $wherebill['id'] = $id;
        $bill_info = Db::name('bill_info')->where($wherebill)->find();

        //支付开始  1获取用户余额  支付  添加支付记录 改变状态
        $userwhere['id'] = $uid;
        $userinfo = Db::name('user')->where($userwhere)->find();
        if ($userinfo['money'] < $bill_info['sure_charge']) {
            return msg(2, '账户余额不足以支付服务费请充值', $userinfo);
            exit();
        } else {
            $money = $userinfo['money'] * 1 - $bill_info['sure_charge'] * 1;
            $data['money'] = $money;
            $userinfores = Db::name('user')->where($userwhere)->update($data);
            //查找订单
            $orderinfo = Db::name('order_info')->where(['bill_no' => $bill_no])->find();
            //添加服务费支付记录
            $this->addbondlog($bill_info['sure_charge'], $uid, $orderinfo['id'], $bill_no, 3);
            //支付成功后改变票据状态为服务中
            $bill_info_data['status'] = 2;//服务中
            Db::name('bill_info')->where($wherebill)->update($bill_info_data);

            $where['id'] = $orderinfo['id'];
            $order_info_data['status'] = 2;//服务中
            Db::name('order_info')->where($where)->update($order_info_data);
            if ($userinfores) {
                return msg(1, '扣除余额成功', $userinfores);
                exit();
            } else {
                return msg(2, '扣除余额失败', $userinfores);
                exit();
            }
        }
    }


    public function addbondlog($bond, $uid, $orderid, $bill_no, $status)
    {
        //添加扣除记录

        $bondlog['create_time'] = time();
        $bondlog['title'] = '支付服务费';
        $bondlog['money'] = $bond;
        $bondlog['uid'] = $uid;
        $bondlog['orderid'] = $orderid;
        $bondlog['type'] = 1;
        $bondlog['bill_no'] = $bill_no;
        $bondlog['status'] = $status;
        Db::name('bond_log')->insert($bondlog);
    }


    //确认订单成功
    public function orderok()
    {
        $uid = Session::get('uid');
        $id = input('id');
        if ($id == null) {
            return msg(2, '缺少主要参数', $id);
            exit();
        }
        $where['id'] = $id;
        $billdata['status'] = 3;
        Db::name('bill_info')->where($where)->update($billdata);

        $data['status'] = 4;//确认完成
        $data['complete_time'] = time();
        $orderinfo = Db::name('order_info')->where($where)->update($data);
        if ($orderinfo) {
            return msg(1, '确认完成', $orderinfo);
            exit();
        } else {
            return msg(2, '确认完成失败', $orderinfo);
            exit();

        }
    }


}